<?php

namespace App\Http\Controllers\Applet\User;

use App\Exceptions\ApiException;
use App\Exceptions\SmsException;
use App\Services\Applet\User\UserService;
use Exception;
use Illuminate\Http\JsonResponse;

class UserController extends BaseUserController
{

    protected UserService $userService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->userService = $this->loadService('Applet\User\UserService');
    }

    /**
     * 发送短信验证码
     * @return JsonResponse
     * @throws ApiException
     * @throws SmsException
     */
    public function sendVerifyCode(): JsonResponse
    {
        $rule = [
            'phone' => 'required|size:11',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->sendVerifyCode($this->params);
        return outputJsonResult();
    }

    /**
     * 注册
     * @return JsonResponse
     * @throws Exception
     */
    public function register(): JsonResponse
    {
        $rule = [
            'code' => 'required|size:32',
            'phone' => 'required|string|size:11',
            'verifyCode' => 'required|string|size:6',
            'password' => 'required|string|between:6,12',
            'qrPassword' => 'required|string|between:6,12',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->register($this->params);
        return outputJsonResult();
    }

    /**
     * 登录
     * @return JsonResponse
     * @throws Exception
     */
    public function login(): JsonResponse
    {
        $rule = [
            'phone' => 'required|string|size:11',
            'password' => 'required|string|between:6,12',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $data = $this->userService->login($this->params);
        return outputJsonResult($data);
    }

    /**
     * 退出登录
     * @return JsonResponse
     */
    public function logout(): JsonResponse
    {
        $this->userService->logout();
        return outputJsonResult();
    }

    /**
     * 重置密码
     * @return JsonResponse
     * @throws Exception
     */
    public function resetPassword(): JsonResponse
    {
        $rule = [
            'phone' => 'required|string|size:11',
            'verifyCode' => 'required|string|size:6',
            'password' => 'required|string|between:6,12',
            'qrPassword' => 'required|string|between:6,12',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->resetPassword($this->params);
        return outputJsonResult();
    }

    /**
     * 用户详情
     * @return JsonResponse
     * @throws Exception
     */
    public function userInfo(): JsonResponse
    {
        $data = $this->userService->userInfo();
        return outputJsonResult($data);
    }

    /**
     * 订单总数列表
     * @return JsonResponse
     * @throws Exception
     */
    public function orderTotal(): JsonResponse
    {
        $data = $this->userService->orderTotal();
        return outputJsonResult($data);
    }

    /**
     * 认养列表
     * @return JsonResponse
     * @throws Exception
     */
    public function sponsorList(): JsonResponse
    {
        $list = $this->userService->sponsorList($this->params);
        return outputJsonResult($list);
    }

    /**
     * 认养详情
     * @return JsonResponse
     * @throws Exception
     */
    public function sponsorInfo(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $detail = $this->userService->sponsorInfo($this->params);
        return outputJsonResult($detail);
    }

    /**
     * 积分记录列表
     * @return JsonResponse
     * @throws Exception
     */
    public function integralRecordsList(): JsonResponse
    {
        $list = $this->userService->integralRecordsList();
        return outputJsonResult($list);
    }
}
